package com.yangluyao.test.callable.thread;

import java.util.concurrent.*;

/**
 * @author YLY
 * @ClassName MyThreadPoolDemo  第4钟获得/使用Java多线程的方式 线程池
 * @Date 2020/5/22
 * @Version 1.0.2
 */
public class MyThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                1L,
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        try {
            //模拟10个用户办理业务，只有五个窗口
            for (int i = 1; i <= 10; i++) {
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "\t  窗口办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }

    private static void threadPoolInit() {
        //ExecutorService threadPool = Executors.newFixedThreadPool(5); //一池5 个处理流程
        //ExecutorService threadPool = Executors.newSingleThreadExecutor(); //一池1个处理流程
        ExecutorService threadPool = Executors.newCachedThreadPool(); //一池N个处理流程
        try {
            //模拟10个用户办理业务，只有五个窗口
            for (int i = 1; i <= 10; i++) {
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "\t  窗口办理业务");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}
